package com.sun.controller;

import com.sun.entities.Customer;
import com.sun.service.CustomerService;
import com.sun.vo.CustomerVo;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Description:
 * @Author hugh.sun
 * @Date 2021/11/19 11:18
 */
@Api
@Slf4j
@RestController
@RequestMapping("/customer")
public class CustomerController {
    @Autowired
    private CustomerService customerService;
    /*动态sql和模糊查询*/
    @GetMapping("/findCustomer")
    public List<CustomerVo> findCustomer(@Param("CustomerVo") CustomerVo customerVo)
    {
        System.out.println(customerVo);
        List<CustomerVo> customers = customerService.findAllCustomer(customerVo);
        log.debug(customers+"--------------");
        return customers;
    }
    /*验证佛foreach*/
    @GetMapping("/findCustomerByName")
    public List<Customer> findCustomerByName(@Param("usernames")String[] usernames)
    {
        System.out.println(usernames);
        List<Customer> customerByNames = customerService.findCustomerByNames(usernames);
        return customerByNames;
    }
    @PutMapping("/updateCustomerById")
    public String updateCustomer(@Param("customer")Customer customer)
    {
        String msg;
        try {
            customerService.updateCustomer(customer);
            msg = "更新成功";
        } catch (Exception e) {
            e.printStackTrace();
             msg = "更新失败";
        }
        return msg;
    }



}
